﻿namespace Caliburn.Micro
{
    using System;
    using System.IO.IsolatedStorage;
    using System.IO;

    /// <summary>
    /// A logger.
    /// </summary>
    public interface ILog
    {
        /// <summary>
        /// Logs the message as info.
        /// </summary>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        void Info(string format, params object[] args);

        /// <summary>
        /// Logs the message as a warning.
        /// </summary>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        void Warn(string format, params object[] args);

        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        void Error(Exception exception);
    }

    /// <summary>
    /// Used to manage logging.
    /// </summary>
    public static class LogManager
    {
        static readonly ILog NullLogInstance = new NullLog();
        //static readonly ILog IOLogInstance = new IOLog();

        /// <summary>
        /// Creates an <see cref="ILog"/> for the provided type.
        /// </summary>
        public static Func<Type, ILog> GetLog = type => NullLogInstance;

        private class NullLog : ILog
        {
            public void Info(string format, params object[] args) { }
            public void Warn(string format, params object[] args) { }
            public void Error(Exception exception) { }
        }

        //private class IOLog : ILog
        //{
        //    IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication();

        //    private string logTxt = "log.txt";
        //    private IsolatedStorageFileStream stream;
        //    StreamWriter sw;
        //    public IOLog()
        //    {
        //        stream = isFile.OpenFile(logTxt, System.IO.FileMode.OpenOrCreate);
        //        sw = new StreamWriter(stream);
        //    }

        //    public void Info(string format, params object[] args)
        //    {
        //        sw.WriteLine("Info:"+string.Format(format, args));
        //    }
        //    public void Warn(string format, params object[] args) {
        //        sw.WriteLine("Warn:"+string.Format(format, args));
        //    }
        //    public void Error(Exception exception) {
        //        sw.WriteLine("Error:"+exception.Message);
        //    }

        //    public void Close()
        //    {
        //        sw.Close();
        //        stream.Close();
        //    }
        //}
    }
}